var ck_shift_to_tab = 1;
var ck_shift_to_movefocus = 2;

$('textarea').keydown(function(e){

	console.log('got here with ' + e.keyCode);
	
	if (e.keyCode == 9) {	
		o = localStorage['textarea-tabbing'] ? localStorage['textarea-tabbing'] : ck_shift_to_tab; 		
		if ((o == ck_shift_to_tab && e.shiftKey) || 
			(o == ck_shift_to_movefocus && e.shiftKey == false)) {
			insertAtCursor($(this).get(0),'\t');
			return false;
		}			
	}			
	
	return true;
});

function insertAtCursor(el,text) {		
    if (el.selectionStart || el.selectionStart == '0') {
        var startPos = el.selectionStart;
        var endPos = el.selectionEnd;
        el.value = el.value.substring(0, startPos) + text + el.value.substring(endPos, el.value.length);
    } 
	else {
        el.value += text;
    }
}	
	